[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
strtol() Convert String to Long Decimal Integer
#include <stdlib.h>
long strtol(nptr,endptr,base);
const char *nptr; Pointer to string
char **endptr; Pointer to end of scan
int base; Number base to use
strtol() converts a string pointed to by 'nptr' to a long-integer
value. The string is a sequence of characters that can be
interpreted as a long-integer value. strtol() stops reading the
string at the first character that cannot be recognized as part of a
long-integer value. 'endptr' points to the character that stopped the
scan. This character could be the null character that terminated the
string or the first numeric character greater than or equal to
'base'.
The string must match this sequence:
[whitespace] [sign] [0] [x] [digits]
If 'base' is between 2 and 36, then 'base' is the base of the
long-integer represented in 'nptr'. If 'base' is 0, the first few
characters of 'nptr' determine the base: If the first character is 0
and the second character is '1'-'7', the value is an octal integer;
if the first character is 0 and the second character is 'x' or 'X',
the value is a hexadecimal integer; if the first character is '1'-
'9', the value is a decimal integer.
Returns: The long value represented by the string.
-------------------------------- Example ---------------------------------
The following statements convert 'string' to a long integer.
#include <stdlib.h>
#include <stdio.h>
int bas2 = 2;
int bas8 = 8;
int bas16 = 16;
char *string = "12050a03";
long longint;
char *termn;
main()
{
longint = strtol(string,&termn,bas2);
printf("%s (base %d) = %ld\n",string,bas2,longint);
longint = strtol(string,&termn,bas8);
printf("%s (base %d) = %ld\n",string,bas8,longint);
}
See Also:
strtod()
atof()
atol()
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson